import request from '@/utils/request';
import { API_PATH_PRE } from '@/generic/ConfigParams';

export async function updateUserInfo(params, userId=undefined, key = "user") {
    if(userId) {
        const apiUrl = `${API_PATH_PRE}${key}/${userId}`;
        console.log("===>> apiUrl", apiUrl)
        return request(apiUrl, {
            method: 'PATCH',
            headers: {
                'Authorization': `Bearer ${localStorage.getItem("token")}`,
                'Content-Type': "application/json; charset=UTF-8",
            },
            data: {...params},
            parseResponse: false,
        });
    }
    console.error("userId is undefined ! ");
    return [];
}

const AVATAR = 'avatar';
const USER = 'user';
export async function GetAvatar(params = {}, parse = false) {  // user/%d/avatar
    const uid = params.id;
    if (uid) {
        const apiUrl = `${API_PATH_PRE}${USER}/${uid}/${AVATAR}`;
        console.log("===>> apiUrl", apiUrl)
        return request(apiUrl, {
            headers: {
                'Authorization': `Bearer ${localStorage.getItem("token")}`
            },
            parseResponse: parse,
        });
    }

    return false;
}
export async function UploadAvatar(params = {}, parse = false) {  // user/%d/avatar
    const { id:uid, file } = params;
    if (uid && file) {
        const apiUrl = `${API_PATH_PRE}${USER}/${uid}/${AVATAR}`;
        console.log("===>> apiUrl", apiUrl)
        return request(apiUrl, {
            method: 'POST',
            headers: {
                'Authorization': `Bearer ${localStorage.getItem("token")}`
            },
            data: file,
            parseResponse: parse,
        });
    }

    return false;
}
